module.exports = (app) => {
    const { STRING, INTEGER, DATE, JSON } = app.Sequelize;

    const EnterpriseCertificate = app.model.define(
        "enterprise-certificate",
        {
            id: {
                type: INTEGER,
                primaryKey: true,
                autoIncrement: true,
            },
            enterpriseName: {
                type: STRING(255),
                allowNull: false,
            },

            certificateTypeName: {
                type: STRING(255), 
            },
            nationalLevel: {
                type: STRING(255), 
            },
            certificateNumber: {
                type: STRING(255),
                allowNull: false,
                unique: true,
                validate: {
                    custom: async (certificateNumber) => {
                      const certificate = await EnterpriseCertificate.findOne({ where: { certificateNumber } });
                      if (certificate) {
                        throw new Error("此证书已存在");
                      }
                    },
                  },
            },
            annualInspectionYears: {
                type: JSON,
            },
            validityStartDate: {
                type: DATE, 
                defaultValue: null,
                allowNull: true,
            },
            validityEndDate: {
                type: DATE, 
                defaultValue: null,
                allowNull: true,

            },
            remarks: {
                type: STRING(500), 
            },
            isDeleted: {
                type: INTEGER,
                defaultValue: 0,
            },
        },
        {
            timestamps: true,
            underscored: false,
        }
    );

    return EnterpriseCertificate;
};